<?php

namespace Home\Controller;

use Think\Controller;
use Common\Controller\DefaultController;
use Admin\Model\ColumnModel;

class FarmController extends DefaultController {

    //显示淘金农场首页
    public function index() {
        $member = M('Member');
        $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();

        $land = M('Land');
        $landDataCount = $land->where(array('land_member_id'=>$_SESSION['member_id']))->count();
        $landData = $land->where(array('land_member_id'=>$_SESSION['member_id']))->select();

        $this->assign('memberData',$memberData);
        $this->assign('landDataCount',$landDataCount);
        $this->assign('landData',$landData);
        $this->assign('commodityData',$this->getCommodityData());
        $this->assign('warehouseData',$this->getWarehouseData());

        $this->display();
    }

    //背景音乐开关
    public function farmBgMusic() {
        $isPlay = I('isPlay');
        session('musicSwitch', $isPlay);
        exit;
    }

    //公告
    public function noticeList() {
        $notices = M('notice');
        $this->assign('notices', $notices->select());
        $this->display('show');
    }

    //商店商品数据
    public function getCommodityData(){

        $shopCommodity = M('ShopCommodity');

        //$typeId = I('typeId');

        //$where['com_recommend'] = array('LIKE','%'.$typeId.'%');

        $shopCommodityData = $shopCommodity->/*where($where)->*/select();

        foreach ($shopCommodityData as $k=>$v) {

            $shopCommodityData[$k]['com_sum'] = $shopCommodityData[$k]['com_sum'] / 10000;

        }

        return $shopCommodityData;

        /*echo json_encode($shopCommodityData);
        exit;*/

    }

    //商店商品购买
    public function buyCommodity(){

        $cId = I('commodityId');

        $member = M('Member');
        $shopCommodity = M('ShopCommodity');
        $memberWarehouse = M('MemberWarehouse');

        $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->getField('member_gold');
        $shopCommodityData = $shopCommodity->where(array('id'=>$cId))->field('com_count,com_price,com_sum,com_type')->find();

        if($memberData < $shopCommodityData['com_price']){
            echo json_encode(array('reTips'=>'Error','reMsg'=>'购买失败，钻石不足'));
            exit;
        }

        $mNewData['member_id'] = $_SESSION['member_id'];
        $mNewData['member_gold'] = $memberData - $shopCommodityData['com_price'];

        $mWarehouseData['ware_member_id'] = $_SESSION['member_id'];
        //$mWarehouseData['ware_number'] = $_SESSION['member_id'];
        $mWarehouseData['ware_type'] = $shopCommodityData['com_type'];
        $mWarehouseData['ware_goods_id'] = $cId;
        $mWarehouseData['ware_goods_count'] = $shopCommodityData['com_count'];
        $mWarehouseData['status'] = 0;
        $mWarehouseData['add_time'] = time();

        $sNewComCommodityData['id'] = $cId;
        $sNewComCommodityData['com_sum'] = $shopCommodityData['com_sum'] - $shopCommodityData['com_count'];

        //memberDetail($shopCommodityData['com_price'],'gold', '购买种子');

        $member->save($mNewData);
        $shopCommodity->save($sNewComCommodityData);
        $memberWarehouse->add($mWarehouseData);

        echo json_encode(array('reTips'=>'Success','reMsg'=>'购买成功'));
        exit;

    }

    //会员仓库数据
    public function getWarehouseData(){

        $memberWarehouse = M('MemberWarehouse');

        $memberWarehouseData = $memberWarehouse->where(array('ware_member_id'=>$_SESSION['member_id']))->select();

        foreach ($memberWarehouseData as $k=>$v){

            $memberWarehouseData[$k]['com_img'] = M('ShopCommodity')->where(array('id'=>$memberWarehouseData[$k]['ware_goods_id']))->getField('com_img');

        }

        return $memberWarehouseData;

    }

}
